﻿@model IList<Commission.Models.Product>
@using MyResources = Commission.Resources.AppResource

@{
    ViewBag.Title = "Index";
}

<script>
    function calculate(obj, rowIndex) 
    {
        var total = 0;

        var rows = $("#row").length;
        var quantity = obj.value;

        var price = $("#price" + rowIndex).val();
        var amount = quantity * price;
        var total = $("#total" + rowIndex);
        total.val(amount);
     
        for (i = 0; i <= rows; i++) {
            
        }
    }
</script>

<h3>@MyResources.LabelOrder</h3>

@if (User.IsInRole("Admin")) {
<p>
    @Html.ActionLink("Create New", "Create")
</p>
}

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <p>
        @Html.ValidationMessageFor(model => Model)
    </p>

<table class="table">
    <tr>
        <th style="width:80px">
            @MyResources.LabelProductNo
        </th>
        <th style="width:210px">
            @MyResources.LabelProductName
        </th>
        <th style="width:120px">
            @MyResources.LabelProductImage
        </th>
        <th class="text-center" style="width:100px" >
            @MyResources.LabelKHPrice
        </th>
        
        <th style="width:100px">
            @MyResources.LabelUnit
        </th>
        <th class="text-center" style="width:100px">
            @MyResources.LabelPPPoint
        </th>
        <th class="text-center" style="width:100px">
            @MyResources.LabelQuantity
        </th>

        <th class="text-center" style="width:100px">
            @MyResources.LabelTotal
        </th>

        <th class="text-center">
            @MyResources.LabelNote
        </th>

        @if (User.IsInRole("Admin")) {
            <th></th>
        }
    </tr>

    @for (int i = 0; i < Model.Count(); i++) {
    <tr>
        
        <td style="padding-top:14px;">
            @Html.DisplayFor(modelItem => Model[i].ProductNo)
        </td>
        <td style="padding-top:14px;">
            @Html.DisplayFor(modelItem => Model[i].ProductName)
        </td>
        <td>
            @if (Model[i].FileId != null && Model[i].FileId != 0)
            { 
                <img src="@Url.Content(Model[i].FileUrl)" height="100" width="100" alt="Image" />
            }
        </td>
        <td class="text-right" style="padding-top:14px;">
            @Html.DisplayFor(modelItem => Model[i].Price)
            @Html.HiddenFor(modelItem => Model[i].Price,
                new {
                    @id = "price" + i
                })
        </td>

        <td class="text-center" style="padding-top:14px;">
            @Html.DisplayFor(modelItem => Model[i].UnitName)
        </td>

        <td class="text-right" style="padding-top:14px;">
            @Html.DisplayFor(modelItem => Model[i].PPPoint)
        </td>

        <td>
            @Html.TextBoxFor(modelItem => Model[i].Quantity, 
                new { 
                    @class = "text-right form-control", 
                    @onchange="calculate(this, " + i + ")",
                    @id = "quantity" + i
                })
        </td>

        <td>
            @Html.TextBoxFor(modelItem => Model[i].Amount, 
                new { 
                    @class = "text-right form-control", 
                    @readonly = "readonly",
                    @id = "total" + i 
                })
        </td>

        <td>
            @Html.DisplayFor(modelItem => Model[i].Note)
        </td>

        @Html.Hidden("row", i)
        @Html.HiddenFor(modelItem => Model[i].ProductId)
        @Html.HiddenFor(modelItem => Model[i].ProductNo)
        @Html.HiddenFor(modelItem => Model[i].ProductName)
        @Html.HiddenFor(modelItem => Model[i].Price)
        @Html.HiddenFor(modelItem => Model[i].UnitId)
        @Html.HiddenFor(modelItem => Model[i].UnitName)
        @Html.HiddenFor(modelItem => Model[i].PPPoint)
        @Html.HiddenFor(modelItem => Model[i].FileId)
        @Html.HiddenFor(modelItem => Model[i].File.File)
    </tr>
    }
</table>

    <p style="width:100%">
        <input type="submit" id="btnPurchase"
               value="@MyResources.LabelPurchase" class="btn btn-default"
               style="margin-left:80%; width:120px;" />
    </p>
}



